package com.jm.web.form;

import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.jsp.JspWriter;


import com.jm.utils.AttributeBuilder;
import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.BasePage;
import com.jm.web.form.support.FieldItem;


/**
 * @author yanjie
 *
 * 2011/9/19 上午11:27:00
 * 按钮控件
 */
public class Button extends FieldItem{
	/**
	 * @param tag
	 */
	public Button() {}

	public  final static String TAG_ITEMNAME 	= "button";
	private final static String TAG_ONCLICK 	= "onclick";
	private final static String TAG_VALUE 		= "value";
	private final static String TAG_ICON 		= "icon";
	private final static String TAG_OPTION 		= "option";
	private final static String TAG_WIDTH 		= "width";//寬度交由button自己處理
	private final static String TAG_MENUWIDTH 		= "menuwidth";
	
	protected String strValue = "";
	protected String strOnClick = "";
	protected String strIcon = "";
	protected String strOption = "";
	protected String strBtnWidth = "";
	protected String strMenuWidth = "";
	
	

	Map option = null;
	
	public void init(){
		this.setNodeName("button");
		this.appendCss("menu-active-btn");
	}
	
	@Override
	public void OnSetProperty(String strName, String strValue) {
		if (strName.compareToIgnoreCase(TAG_ONCLICK) == 0)
		{
			this.strOnClick = strValue;
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_VALUE) == 0)
		{
			this.strValue = strValue;
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_ICON) == 0)
		{
			this.strIcon = strValue;
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_WIDTH) == 0)
		{
			this.strBtnWidth = strValue;
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_OPTION) == 0)
		{
			this.strOption = strValue;
			if(!StringHelper.IsNullOrEmpty(this.strOption)){
				this.setOption(Utils.MenuStringToMap(this.strOption));
			}
			return;
		}
		
		if (strName.compareToIgnoreCase(TAG_MENUWIDTH) == 0)
		{
			this.strMenuWidth = strValue;
			return;
		}
		super.OnSetProperty(strName, strValue);
	}
	
	@Override
	public String getCss() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getCss());
		if(!StringHelper.IsNullOrEmpty(getIcon())){
			//sb.append(" ").append(getIcon());
		}
		
		if(this.isDisabled()){
			sb.append(" ax-btn-disabled");
		}
		return sb.toString();
	}
	
	@Override
	public void outItemBegin(JspWriter writer) {
		try {
			writer.write("<");
			writer.write(StringHelper.Format("table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" type=\"button\" %1$s ",this.GetExtValue("extInfo","")));
			
			AttributeBuilder attributesBuilder = new AttributeBuilder();
			attributesBuilder.InitFromHashtable(this.extAttrList,true);
			FillAttributeBuilder(attributesBuilder);
			attributesBuilder.Remove("extInfo");
			writer.write(attributesBuilder.ToOutputString());
			writer.write("><tr>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void outItemBody(JspWriter writer) {
		
		StringBuilderEx sb = new StringBuilderEx();
		//button 部分
		sb.Append("<td id=\"%1$s-btn\" enab=\"%2$s\" style=\"border:0px !important; padding-left:2px;padding-top:0px;padding-bottom:0px;\" %3$s %4$s >",
				this.getId(),
				this.isEnab(),
				this.hasOption() ? "" : StringHelper.Format("onclick=\"var params = {}; %1$s;\"\"",this.getOnClick()),
				this.GetExtValue("extAtr", "")
				);
			sb.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" ><tr>");
			if(!StringHelper.IsNullOrEmpty(this.getIcon()))
				sb.Append("<td nowrap=\"nowrap\" style=\"border:0px !important;white-space: nowrap;padding:0px;\"><div class=\"%1$s sx-bt-icon\" style=\"font-size:9px;width:15px;height:15px;float:left;margin-bottom: 2px\">&nbsp;</div></td>",this.getIcon());
			sb.Append("<td nowrap=\"nowrap\" style=\"border:0px !important;white-space: nowrap;padding:0px;padding-left: 2px;\" class=\"sx-bt-text\" style=\"padding-top:2px;padding-left:2px;\">%1$s</td>",
					this.getValue()
							);
			sb.Append("</tr></table>");
		sb.Append("</td>");
		
		//button 菜單部分
		if(this.hasOption()){
			sb.Append("<td id=\"%1$s-menu\" nowrap=\"nowrap\" align=\"right\" >",this.getId());
			sb.Append("<div class=\"ax-btn-split\" style=\"font-size:9px;width:14px;height:15px;float:rigth;margin-bottom: 2px;\">&nbsp;</div>");
			sb.Append("<div id=\"%1$s-option\" btnid=\"%1$s\" style=\"display:none;\">%2$s</div>",this.getId(),Utils.MapToMenuHtml(this.option,this));
			sb.Append("</td>");
			
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,StringHelper.Format("$('td[id=%1$s-btn]','div#north').bind('picked',function(event,params){%2$s;});",this.getId(),this.getOnClick()));
			//註冊菜单JS
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, PageJsGear.getToolbarMenuJs(this.getBaseTag().getPage(), this));
		}
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void outItemEnd(JspWriter writer) {
		try {
			writer.write("</tr></table>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void FillAttributeBuilder(AttributeBuilder attributesBuilder) {
		super.FillAttributeBuilder(attributesBuilder);
		
		if(this.isDisabled()){
			attributesBuilder.Set("enab", "false");
		}
		
		attributesBuilder.Remove("value");
		
		if(this.hasOption()){
			String strWidth = this.getBtnWidth().replace("px", "");
			if(!this.isFromTag())
			{
				strWidth = StringHelper.getValue(strWidth, "48");
				strWidth = StringHelper.getValue(Integer.parseInt(strWidth)+10);
				this.setBtnWidth(strWidth+"px");
				
				attributesBuilder.Append(TAG_STYLE,
						StringHelper.Format("width:%1$s;",this.getBtnWidth()));
			}
			attributesBuilder.Append("width", this.getBtnWidth());
		}
	}

	public boolean hasOption(){
		return ((this.option != null && this.option.size() > 0) || !StringHelper.IsNullOrEmpty(strOption));
	}
	
	public String toButtonString(){
		StringWriter writer = new StringWriter();
		writer.write(StringHelper.Format("<%1$s ",this.getNodeName()));
		AttributeBuilder attributesBuilder = new AttributeBuilder();
		attributesBuilder.InitFromHashtable(this.extAttrList,true);
		FillAttributeBuilder(attributesBuilder);
		
		
		if(!StringHelper.IsNullOrEmpty(this.getIcon())){
			attributesBuilder.Append("class",this.getIcon());
		}
		
		if(!StringHelper.IsNullOrEmpty(this.getOnClick())){
			attributesBuilder.Set("onclick",this.getOnClick());
		}
		
		writer.write(attributesBuilder.ToOutputString());
		
		writer.write(" >");
		if(!StringHelper.IsNullOrEmpty(this.getValue())){
			writer.write(this.getValue());
		}
		writer.write("</button>");
		
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<table enab=\"%1$s\" class=\"xt-btn-icon \" onmousedown=\"if($(this).isactive())$(this).addClass('ax-btn-pressed');\" onmouseup=\"if($(this).isactive())$(this).removeClass('ax-btn-pressed');\"  onmouseover=\"if($(this).isactive())$(this).addClass('ax-btn-over');\" onmouseout=\"if($(this).isactive())$(this).removeClass('ax-btn-over').removeClass('ax-btn-pressed');\" cellspacing=\"0\" border=\"0\"><tbody>",this.isEnab());
		sb.Append("<tr class=\"ax-tr-nopadding\"><td class=\"ax-btn-mc\">");
		sb.Append(writer.toString());
		sb.Append("</td></tr>");
		sb.Append("</tbody></table>");
		return sb.toString();
	}
	
	public String getOnClick() {
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("if($('table[id=%1$s]').attr('enab') == 'false'){",this.getId());
		sb.Append("return false;}");
		sb.Append("else{%1$s;",this.strOnClick);
		sb.Append("return false;",this.getId());
		sb.Append("}");
		return sb.toString();
	}
	
	public boolean isNullClick(){
		return StringHelper.IsNullOrEmpty(this.strOnClick);
	}
	
	
	public void setOnClick(String strOnClick) {
		this.strOnClick = strOnClick;
	}

	public String getValue() {
		return strValue;
	}

	public void setValue(String strValue) {
		this.strValue = strValue;
	}

	public String getIcon() {
		return strIcon;
	}

	public void setIcon(String strIcon) {
		this.strIcon = strIcon;
	}
	
	public void appendCss(String strCss){
		this.setCss(StringHelper.Format("%1$s %2$s",this.getCss(),strCss));
	}
	
	public String getBtnWidth() {
		return strBtnWidth;
	}

	public void setBtnWidth(String strBtnWidth) {
		this.strBtnWidth = strBtnWidth;
	}
	
	public Map getOption() {
		if(option == null)
			option = new LinkedHashMap();
		return option;
	}

	public void setOption(Map option) {
		this.option = option;
	}
	
	public void setOption(Object option) {
		if(option == null || !(option instanceof Map))
			return;
		this.option = (Map)option;
	}
	
	public String getMenuWidth() {
		return strMenuWidth;
	}

	public void setMenuWidth(String strMenuWidth) {
		this.strMenuWidth = strMenuWidth;
	}
}
